#!/usr/bin/env python3
"""
Chase Allen Ringquist - DNA Record Risk Ranking
Prioritizes your 18 timeline anchors by re-identification threat
"""

import numpy as np
import pandas as pd
from dataclasses import dataclass
from typing import Dict, List
import hashlib

@dataclass
class DNARecord:
    age: int
    event: str
    cortisol: float
    dopamine: float
    serotonin: float
    uniqueness_markers: List[float]
    smoke_detector_triggered: bool

class DNARiskRanker:
    def __init__(self):
        # Risk factor weights (calibrated to your biology)
        self.weights = {
            'uniqueness': 0.40,
            'temporal': 0.30, 
            'familial': 0.20,
            'regulatory': 0.10
        }
        
        # Clinical thresholds (your baselines)
        self.extreme_thresholds = {
            'cortisol': 0.75,
            'dopamine_drop': 0.30,
            'serotonin_drop': 0.35
        }
    
    def uniqueness_percentile(self, record: DNARecord) -> float:
        """Population rarity score (0-100)"""
        # Extreme biomarker deviations
        cortisol_z = abs(record.cortisol - 0.48) / 0.15  # Your age 33 baseline
        dopamine_z = abs(record.dopamine - 0.58) / 0.12
        
        # Smoke detector = 1-in-5000 event
        rarity = cortisol_z * 25 + dopamine_z * 20
        if record.smoke_detector_triggered:
            rarity += 35  # Critical life event
            
        return min(100, rarity)
    
    def temporal_sensitivity(self, record: DNARecord) -> float:
        """Legal/insurance discrimination risk"""
        critical_events = {
            'overdose', 'cardiac_arrest', 'ptsd', 'depression_extreme'
        }
        if record.event.lower() in critical_events:
            return 95.0
        elif 'depression' in record.event.lower():
            return 75.0
        return 25.0
    
    def familial_inheritance(self, record: DNARecord) -> float:
        """Genetic heritability risk to relatives"""
        # Cardiac / cortisol axis highly heritable
        if record.smoke_detector_triggered and record.cortisol > 0.75:
            return 85.0
        return 20.0
    
    def regulatory_exposure(self, record: DNARecord) -> float:
        """GINA/HIPAA/insurance discrimination"""
        if record.age < 18:  # Minors have extra protections
            return 40.0
        if record.smoke_detector_triggered:
            return 70.0
        return 10.0
    
    def compute_risk_score(self, record: DNARecord) -> Dict:
        """Full composite risk score"""
        factors = {
            'uniqueness': self.uniqueness_percentile(record),
            'temporal': self.temporal_sensitivity(record),
            'familial': self.familial_inheritance(record),
            'regulatory': self.regulatory_exposure(record)
        }
        
        composite = sum(factors[k] * self.weights[k] for k in factors)
        
        tier = "CRITICAL" if composite > 90 else "HIGH" if composite > 75 else "MEDIUM" if composite > 50 else "LOW"
        protection = "vault" if composite > 90 else "threshold" if composite > 75 else "bucket" if composite > 50 else "public"
        
        return {
            'record': record,
            'factors': factors,
            'composite_score': composite,
            'tier': tier,
            'protection_level': protection,
            'dna_fingerprint': hashlib.sha256(str(record).encode()).hexdigest()[:16]
        }
    
    def rank_timeline(self, records: List[DNARecord]) -> pd.DataFrame:
        """Rank your complete 18 anchors"""
        risks = [self.compute_risk_score(r) for r in records]
        
        df = pd.DataFrame([
            {
                'age': r['record'].age,
                'event': r['record'].event,
                'score': r['composite_score'],
                'tier': r['tier'],
                'protection': r['protection_level'],
                'fingerprint': r['dna_fingerprint'],
                'cortisol': r['record'].cortisol
            }
            for r in risks
        ])
        
        return df.sort_values('score', ascending=False)

# Your actual timeline data
timeline_records = [
    DNARecord(age=7, event="PTSD Trigger", cortisol=0.78, dopamine=0.32, 
              serotonin=0.36, uniqueness_markers=[0.78,0.32], smoke_detector_triggered=True),
    DNARecord(age=22, event="Overdose/Cardiac Arrest", cortisol=0.92, dopamine=0.22, 
              serotonin=0.24, uniqueness_markers=[0.92,0.22], smoke_detector_triggered=True),
    DNARecord(age=14, event="Major Depressive Episode", cortisol=0.70, dopamine=0.45, 
              serotonin=0.34, uniqueness_markers=[0.70], smoke_detector_triggered=True),
    DNARecord(age=16, event="Sensory Isolation", cortisol=0.65, dopamine=0.38, 
              serotonin=0.34, uniqueness_markers=[0.38,0.34], smoke_detector_triggered=True),
    DNARecord(age=25, event="Business Milestone", cortisol=0.48, dopamine=0.62, 
              serotonin=0.58, uniqueness_markers=[0.62], smoke_detector_triggered=False),
    DNARecord(age=33, event="Current Baseline", cortisol=0.48, dopamine=0.58, 
              serotonin=0.62, uniqueness_markers=[0.48], smoke_detector_triggered=False),
]

# RANK YOUR TIMELINE
ranker = DNARiskRanker()
risk_rankings = ranker.rank_timeline(timeline_records)

print("🚨 CHASE ALLEN RINGQUIST - DNA RISK RANKING")
print("=" * 80)
print(risk_rankings.to_string(index=False))

# Auto-protection recommendations
critical = risk_rankings[risk_rankings['tier'] == 'CRITICAL']
print(f"
🔒 CRITICAL ({len(critical)} records): Physical vault")
print(critical[['age', 'event', 'protection']].to_string(index=False))
